<!-- CommentModule.vue -->
<template>
  <div class="container">
    <div style="margin-left: 30px" class="comment" v-for="item in comments" :key="item.id">
      <div style="width: 200px" class="info" :id="item.id">
        <el-avatar v-if="item.avatar !== '' && item.avatar != null" :src="item.avatar" />
        <el-avatar v-else icon="el-icon-user-solid" />
        <div class="right">
          <div class="name">{{ item.createBy }}</div>
          <div class="date">{{ item.createTime }}</div>
        </div>
      </div>
      <div  class="content">{{ item.content }}</div>
      <div class="control">
        <span
            class="like"
            :class="{ active: item.isLike }"
            @click="likeClick(item)"
        >
          <svg-icon icon-class="like" />
          <span class="like-num" style="margin-left: 5px;">
            {{ item.likeNum > 0 ? item.likeNum + '人赞' : '赞' }}
          </span>
        </span>
        <span class="comment-reply" @click="showCommentInput(item)">
          <svg-icon icon-class="comment" />
          <span style="margin-left: 5px;">回复</span>
        </span>
      </div>
      <div  class="reply" >
        <div class="item" v-for="reply in item.children" :key="reply.id">
          <div class="reply-content">
            <span class="from-name">{{ reply.createBy }}</span><span>: </span>
            <span class="to-name" v-show="reply.parentId !== reply.mainId"
            >@{{ reply.createBy }}</span
            >
            <span v-show="reply.delFlag === '0'">{{ reply.content }}</span>
            <span v-show="reply.delFlag === '1'" style="color: #909399;"
            >该评论已被删除！</span
            >
          </div>
          <div class="reply-bottom">
            <span>{{ reply.createTime }}</span>
            <span class="reply-text" @click="showCommentInput(item, reply)">
              <svg-icon icon-class="comment" />
              <span style="margin-left: 5px;">回复</span>
            </span>
          </div>
        </div>
        <div class="write-reply" v-if="item.children !== null" @click="showCommentInput(item)">
          <i class="el-icon-edit"></i>
          <span class="add-comment">添加新评论</span>
        </div>
        <input-component
            :show="showItemId === item.id"
            :value="inputComment"
            :toComment="name"
            :toId="id"
            @cancel="cancelInput"
            @confirm="commitComment"
        ></input-component>
      </div>
    </div>
  </div>
</template>

<script setup>

import InputComponent from './InputComponent';
import {
  getToken
} from '@/utils/auth'
import {
  addCmsCommentLike,
  delCmsCommentLike,
} from '@/api/cms/comment';
import useUserStore from "@/store/modules/user";

const userStore = useUserStore();



const inputComment = ref('');
const name = ref('');
const id = ref(null);
const showItemId = ref('');
const { comments } = defineProps(['comments']);

function reset() {
  inputComment.value = '';
  name.value = '';
  id.value = null;
  showItemId.value = '';
}

async function addCommentLike(item) {
  const token = getToken();
  reset();
  const commentLikeForm = {
    commentId: item.id,
    userId: null,
    likeNum: item.likeNum,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
  };

  if (token === null || token === '') {
    commentLikeForm.createBy = '匿名用户';
  } else {
    commentLikeForm.createBy = userStore.userName;
  }

  await addCmsCommentLike(commentLikeForm);
}

async function delCommentLike(item) {
  const token = getToken();
  reset();
  const commentLikeForm = {
    commentId: item.id,
    userId: null,
    likeNum: item.likeNum,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
  };

  if (token === null || token === '') {
    commentLikeForm.createBy = '匿名用户';
  } else {
    commentLikeForm.createBy = userStore.userName;
  }

  await delCmsCommentLike(commentLikeForm);
}

function likeClick(item) {
  if (item.isLike === null) {
    Vue.$set(item, 'isLike', true);
    item.likeNum++;
    addCommentLike(item);
  } else {
    if (item.isLike) {
      item.likeNum--;
      delCommentLike(item);
    } else {
      item.likeNum++;
      addCommentLike(item);
    }
    item.isLike = !item.isLike;
  }
}

function cancelInput() {
  showItemId.value = '';
}

/*function commitComment() {
  debugger
  $emit('replyConfirm', { inputComment: inputComment.value, id: id.value });
  inputComment.value = '';
}*/

function commitComment(value) {
  $emit('replyConfirm', value);
}


function showCommentInput(item, reply) {
  if (reply) {
    inputComment.value = '';
    name.value = '回复@' + reply.createBy + ':';
    id.value = reply.id;
  } else {
    inputComment.value = '';
    name.value = '写下你的评论';
    id.value = item.id;
  }
  showItemId.value = item.id;
}


</script>

<style scoped rel="stylesheet/scss"  lang="scss">

.container {
  padding: 0 10px;
  box-sizing: border-box;
  .comment {
    display: flex;
    flex-direction: column;
    padding: 10px;
    border-bottom: 1px solid #F2F6FC;
    .info {
      display: flex;
      align-items: center;
      .right {
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        .name {
          font-size: 16px;
          color: #303133;
          margin-bottom: 5px;
          font-weight: 500;
        }
        .date {
          font-size: 12px;
          color: #909399;
        }
      }
    }
    .content {
      font-size: 16px;
      color: #303133;
      line-height: 20px;
      padding: 10px 0;
    }
    .control {
      display: flex;
      align-items: center;
      font-size: 14px;
      color: #909399;
      .like {
        display: flex;
        align-items: center;
        margin-right: 20px;
        cursor: pointer;
        &.active, &:hover {
          color: #409EFF;
        }
        .iconfont {
          font-size: 14px;
          margin-right: 5px;
        }
      }
      .comment-reply {
        display: flex;
        align-items: center;
        cursor: pointer;
        &:hover {
          color: #333;
        }
        .iconfont {
          font-size: 16px;
          margin-right: 5px;
        }
      }

    }
    .reply {
      margin: 10px 0;
      border-left: 2px solid #DCDFE6;
      .item {
        margin: 0 10px;
        padding: 10px 0;
        border-bottom: 1px dashed #EBEEF5;
        .reply-content {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: #303133;
          .from-name {
            color: #409EFF;
          }
          .to-name {
            color: #409EFF;
            margin-left: 5px;
            margin-right: 5px;
          }
        }
        .reply-bottom {
          display: flex;
          align-items: center;
          margin-top: 6px;
          font-size: 12px;
          color: #909399;
          .reply-text {
            display: flex;
            align-items: center;
            margin-left: 10px;
            cursor: pointer;
            &:hover {
              color: #333;
            }
            .icon-comment {
              margin-right: 5px;
            }
          }
        }
      }
      .write-reply {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: #909399;
        padding: 10px;
        cursor: pointer;
        &:hover {
          color: #303133;
        }
        .el-icon-edit {
          margin-right: 5px;
        }
      }
      .fade-enter-active, fade-leave-active {
        transition: opacity 0.5s;
      }
      .fade-enter, .fade-leave-to {
        opacity: 0;
      }
      .input-wrapper {
        padding: 10px;
        .gray-bg-input, .el-input__inner {
          /*background-color: #67C23A;*/
        }
        .btn-control {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          padding-top: 10px;
          .cancel {
            font-size: 16px;
            color: #606266;
            margin-right: 20px;
            cursor: pointer;
            &:hover {
              color: #333;
            }
          }
          .confirm {
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>
